import CryptoJS from 'crypto-js/crypto-js';
import md5 from 'js-md5';
import { AuthenticationMd5Key } from './user-util';

// 秘钥
export const AuthenticationASEKey = CryptoJS.enc.Utf8.parse('njsa&@Q)$&hu++==f839jdajbdkavuda');

// 偏移量
export const AuthenticationASEIv: string = '1234567890123456';

export const AES_ENCRYPT_PASSWORD = (code: string): string => {
    code = md5(code + AuthenticationMd5Key);
    const encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(code), AuthenticationASEKey, {
        iv: CryptoJS.enc.Utf8.parse(AuthenticationASEIv),
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7,
    });

    return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
};
